package unirio;

import javax.swing.table.DefaultTableModel;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;


@SuppressWarnings("serial")
public class ImpressaoResultados extends JFrame {
	
    private JLabel lblHistescolhas, lblAjuda, lblLoad, lblHeader, lblMenu, lblInfoDist, lblDistancia, lblBackground, lblMapa;

    private JTable table;
    private DefaultTableModel dataModel;
    private Enumeration eCat1,eCat1Key,eCat2,eCat2Key;
    private JButton btnVoltar, btnAvancar;
    private JPanel panel;
	
    public void ImpressaoResultados(Hashtable cat1, Hashtable cat2, int quantidade) {
		
        eCat1 = cat1.elements();
    	eCat1Key = cat1.keys();
        eCat2 = cat2.elements();
        eCat2Key = cat2.keys();
  	  	  
        Object[][] linhas = {};
        Object[] colunas = {"Vendedor", "Total", "Comissao"};
        dataModel = new DefaultTableModel(linhas, colunas);
  	
        while (eCat1.hasMoreElements()) {
            String vendedor = eCat1Key.nextElement().toString();
            Float total = Float.parseFloat(eCat1.nextElement().toString());
            Float comissao = Comissao("1",total);
            if (quantidade>0){
                    total = total/2;
                    comissao = comissao/2;
            }
            dataModel.addRow(new Object[]{vendedor, total, comissao});
            System.out.println("cat1 -> " + vendedor + " - " + total + " - " + comissao);
        }
  	  
        while (eCat2.hasMoreElements()) {
            String vendedor = eCat2Key.nextElement().toString();
            Float total = Float.parseFloat(eCat2.nextElement().toString());
            Float comissao = Comissao("2",total);
            
            if (quantidade>0){
                    total = total/2;
                    comissao = comissao/2;
            }
            
            dataModel.addRow(new Object[]{vendedor, total, comissao});
            System.out.println("cat2 -> " + vendedor + " - " + total + " - " + comissao);
        }

  	  	
        table = new JTable(dataModel);

        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(table);
        this.add(scroll);

        setTitle("Resultado das Consultas");
        setResizable(false);
        setSize(400,400);
	
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowListener());
        setLocationRelativeTo(null); // centraliza a janela 
        setVisible(true);   
    }
	    
    public class WindowListener extends WindowAdapter{  
        public void windowClosing( WindowEvent e ){  
            int opcao= JOptionPane.showOptionDialog(null,   
                "Deseja fechar o Programa?", "Trabalho 1",   
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,   
                null, null, null
            );   

           if (opcao == 0) {   
                   dispose();
           }  
        }  
    }		
	    
    public float Comissao(String cat, float total){

        float comissao = new Float(0.00);

        if (cat.equals("1")){
            if (total <= 1000.00){
                    comissao = new Float(0.10) * total;
            }
            else if (total <= 1800.00){
                    comissao = new Float(100.00) + (new Float(0.15) * (total - new Float(1000.00)));
            }
            else{
                    comissao = new Float(220.00) + (new Float(0.25) * (total - new Float(1800.00)));
            }
        }
        else if (cat.equals("2")){
            if (total <= 2000.00){
                    comissao = new Float(0.10) * total;
            }
            else if (total <= 4000.00){
                    comissao = new Float(0.20) * total;
            }
            else{
                    comissao = new Float(0.30) * total;
            }
        }
        return comissao;
    }
}